#!/bin/bash
#
# lib/senlin
# Install and start **Senlin** service

# To enable, add the following to local.conf
#
# [[local|localrc]]
# enable_plugin senlin https://git.openstack.org/openstack/senlin

# Dependencies:
#
# - functions
# - HORIZON_DIR

# stack.sh
# ---------
# - config_senlin_dashboard
# - configure_senlin
# - cleanup_senlin
# - cleanup_senlin_dashboard
# - create_senlin_cache_dir
# - create_senlin_accounts
# - init_senlin
# - install_senlinclient
# - install_senlin
# - install_senlin_dashboard
# - is_senlin_enabled
# - start_senlin
# - stop_senlin

# Save trace setting
XTRACE=$(set +o | grep xtrace)
set +o xtrace


# Defaults
# --------

# set up default
SENLIN_AUTH_CACHE_DIR=${SENLIN_AUTH_CACHE_DIR:-/var/cache/senlin}
SENLIN_CONF_DIR=/etc/senlin
SENLIN_CONF=$SENLIN_CONF_DIR/senlin.conf
SENLIN_API_HOST=${SENLIN_API_HOST:-$HOST_IP}
SENLIN_API_PORT=${SENLIN_API_PORT:-8778}

SENLIN_DIR=$DEST/senlin
SENLIN_BIN_DIR=$(get_python_exec_prefix)
SENLIN_REPO=${SENLIN_REPO:-${GIT_BASE}/openstack/senlin.git}
SENLIN_BRANCH=${SENLIN_BRANCH:-master}

SENLINCLIENT_DIR=$DEST/python-senlinclient
SENLINCLIENT_REPO=${SENLINCLIENT_REPO:-${GIT_BASE}/openstack/python-senlinclient.git}
SENLINCLIENT_BRANCH=${SENLINCLIENT_BRANCH:-master}

SENLIN_DASHBOARD_DIR=$DEST/senlin-dashboard
SENLIN_DASHBOARD_REPO=${SENLIN_DASHBOARD_REPO:-${GIT_BASE}/openstack/senlin-dashboard.git}
SENLIN_DASHBOARD_BRANCH=${SENLIN_DASHBOARD_BRANCH:-master}


# Functions
# ---------

# Test if any Senlin services are enabled
function is_senlin_enabled {
    [[ ,${ENABLED_SERVICES} =~ ,"sl-" ]] && return 0
    return 1
}

# cleanup_senlin() - Remove residual data files, anything left over from previous
# runs that a clean run would need to clean up
function cleanup_senlin {
    sudo rm -rf $SENLIN_AUTH_CACHE_DIR
    sudo rm -rf $SENLIN_CONF_DIR
}

# configure_senlin() - Set config files, create data dirs, etc
function configure_senlin {

    if [[ ! -d $SENLIN_CONF_DIR ]]; then
        sudo mkdir -p $SENLIN_CONF_DIR
    fi
    sudo install -d -o $STACK_USER $SENLIN_CONF_DIR

    SENLIN_ENGINE_HOST=${SENLIN_ENGINE_HOST:-$SERVICE_HOST}
    SENLIN_ENGINE_PORT=${SENLIN_ENGINE_PORT:-8778}
    SENLIN_API_PASTE_FILE=$SENLIN_CONF_DIR/api-paste.ini
    SENLIN_POLICY_FILE=$SENLIN_CONF_DIR/policy.json

    cp $SENLIN_DIR/etc/senlin/api-paste.ini $SENLIN_API_PASTE_FILE
    cp $SENLIN_DIR/etc/senlin/policy.json $SENLIN_POLICY_FILE

    # common options
    iniset $SENLIN_CONF DEFAULT debug "$ENABLE_DEBUG_LOG_LEVEL"
    iniset $SENLIN_CONF DEFAULT auth_encryption_key $(generate_hex_string 16)
    iniset $SENLIN_CONF DEFAULT default_region_name "$REGION_NAME"

    iniset $SENLIN_CONF DEFAULT use_syslog $SYSLOG
    if [ "$LOG_COLOR" == "True" ] && [ "$SYSLOG" == "False" ]; then
        # Add color to logging output
        setup_colorized_logging $SENLIN_CONF DEFAULT
    fi

    # rpc
    iniset_rpc_backend senlin $SENLIN_CONF

    # OpenStack API
    iniset $SENLIN_CONF senlin_api bind_port $SENLIN_API_PORT

    # Database connection
    iniset $SENLIN_CONF database connection `database_connection_url senlin`

    # Keystone authtoken middleware
    #configure_auth_token_middleware $SENLIN_CONF senlin $SENLIN_AUTH_CACHE_DIR
    iniset $SENLIN_CONF keystone_authtoken identity_uri $KEYSTONE_AUTH_URI
    iniset $SENLIN_CONF keystone_authtoken cafile $SSL_BUNDLE_FILE
    iniset $SENLIN_CONF keystone_authtoken signing_dir $SENLIN_AUTH_CACHE_DIR
    iniset $SENLIN_CONF keystone_authtoken auth_uri $KEYSTONE_AUTH_URI/v3
    iniset $SENLIN_CONF keystone_authtoken admin_user senlin
    iniset $SENLIN_CONF keystone_authtoken admin_password $SERVICE_PASSWORD
    iniset $SENLIN_CONF keystone_authtoken admin_tenant_name $SERVICE_TENANT_NAME

    # Senlin service credentials
    iniset $SENLIN_CONF authentication auth_url $KEYSTONE_AUTH_URI/v3
    iniset $SENLIN_CONF authentication service_username senlin
    iniset $SENLIN_CONF authentication service_password $SERVICE_PASSWORD
    iniset $SENLIN_CONF authentication service_project_name $SERVICE_TENANT_NAME
}

# init_senlin() - Initialize database
function init_senlin {

    # (re)create senlin database
    recreate_database senlin utf8

    $SENLIN_BIN_DIR/senlin-manage db_sync
    create_senlin_cache_dir
}

# create_senlin_cache_dir() - Part of the init_senlin() process
function create_senlin_cache_dir {
    # Create cache dirs
    sudo mkdir -p $SENLIN_AUTH_CACHE_DIR
    sudo install -d -o $STACK_USER $SENLIN_AUTH_CACHE_DIR
}

# install_senlinclient() - Collect source and prepare
function install_senlinclient {
    if use_library_from_git "python-senlinclient"; then
        git_clone $SENLINCLIENT_REPO $SENLINCLIENT_DIR $SENLINCLIENT_BRANCH
        setup_develop $SENLINCLIENT_DIR
    else
        pip_install python-senlinclient
    fi
}

# install_senlin_dashboard() - Collect source and prepare
function install_senlin_dashboard {
    # NOTE(Liuqing): workaround for devstack bug: 1540328
    # https://bugs.launchpad.net/devstack/+bug/1540328
    # where devstack install 'test-requirements' but should not do it
    # for senlin-dashboard project as it installs Horizon from url.
    # Remove following two 'mv' commands when mentioned bug is fixed.

    git_clone $SENLIN_DASHBOARD_REPO $SENLIN_DASHBOARD_DIR $SENLIN_DASHBOARD_BRANCH
    mv $SENLIN_DASHBOARD_DIR/test-requirements.txt $SENLIN_DASHBOARD_DIR/_test-requirements.txt
    setup_develop $SENLIN_DASHBOARD_DIR

    mv $SENLIN_DASHBOARD_DIR/_test-requirements.txt $SENLIN_DASHBOARD_DIR/test-requirements.txt
}

# configure_senlin_dashboard() - Set config files
function config_senlin_dashboard {
    # Install Senlin Dashboard as plugin for Horizon
    ln -sf $SENLIN_DASHBOARD_DIR/_50_senlin.py.example $HORIZON_DIR/openstack_dashboard/local/enabled/_50_senlin.py
}

# cleanup_senlin_dashboard() - Remove residual data files, anything left over from previous
# runs that a clean run would need to clean up
function cleanup_senlin_dashboard {
    sudo rm -rf $HORIZON_DIR/openstack_dashboard/local/enabled/_50_senlin.py
}

# install_senlin() - Collect source and prepare
function install_senlin {
    git_clone $SENLIN_REPO $SENLIN_DIR $SENLIN_BRANCH
    setup_develop $SENLIN_DIR
}

# start_senlin() - Start running processes, including screen
function start_senlin {
    run_process sl-eng "$SENLIN_BIN_DIR/senlin-engine --config-file=$SENLIN_CONF"
    run_process sl-api "$SENLIN_BIN_DIR/senlin-api --config-file=$SENLIN_CONF"
}

# stop_senlin() - Stop running processes
function stop_senlin {
    # Kill the screen windows
    local serv
    for serv in sl-eng sl-api; do
        stop_process $serv
    done
}

# create_senlin_accounts() - Set up common required senlin accounts
function create_senlin_accounts {

    create_service_user "senlin" "admin"

    if [[ "$KEYSTONE_CATALOG_BACKEND" = 'sql' ]]; then

        local senlin_service=$(get_or_create_service "senlin" \
                "clustering" "Senlin Clustering Service")
        get_or_create_endpoint $senlin_service \
            "$REGION_NAME" \
            "$SERVICE_PROTOCOL://$SENLIN_API_HOST:$SENLIN_API_PORT" \
            "$SERVICE_PROTOCOL://$SENLIN_API_HOST:$SENLIN_API_PORT" \
            "$SERVICE_PROTOCOL://$SENLIN_API_HOST:$SENLIN_API_PORT"
    fi

    # get or adds 'service' role to 'senlin' on 'demo' project
    get_or_add_user_project_role "service" "senlin" "demo"
}

# Restore xtrace
$XTRACE

# Tell emacs to use shell-script-mode
## Local variables:
## mode: shell-script
## End:
